﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Graphics.PackedVector;

using WindowsGameInput;



namespace WindowsGame
{
	public interface IObjectLogic
	{
		Matrix Rotation { get; }
		Matrix Translation { get; }
	}


	// OLD...
	public class ObjectLogic : IObjectLogic
	{
		Vector3 position = new Vector3(0.0f, 10.0f, 0.0f);
		Vector3 up = new Vector3(0.0f, 1.0f, 0.0f);
		//Vector3 direction = new Vector3(0.0f, 0.0f, -1.0f);

		Matrix rotation = Matrix.Identity;
		Matrix translation = Matrix.Identity;

		public Matrix Rotation { get { return rotation; } }
		public Matrix Translation { get { return translation; } }

		float upAngle = 0.0f;

		float rotationSpeed = 3.0f;
		float acceleration = 40.0f;
		//float drag = 0.9f;
		float maxSpeed = 1000.0f;
		float currentSpeed = 0.0f;



		public ObjectLogic()
		{
			translation = Matrix.CreateTranslation(position);
		}



		public void Update(GameTime gameTime)
		{
			float dT = (float)gameTime.ElapsedGameTime.TotalSeconds;

			bool rotationOccur = false;

			if(InputHandler.GetState().playerMoveForward)
				currentSpeed += (acceleration - 0.3f * currentSpeed) * dT;
			else
				currentSpeed -= 1.6f * currentSpeed * dT;
			if(InputHandler.GetState().playerMoveBackward)
				currentSpeed -= acceleration * dT;
			if(InputHandler.GetState().playerRotateLeft)
			{
				upAngle += rotationSpeed * dT;
				rotationOccur = true;
			}
			if(InputHandler.GetState().playerRotateRight)
			{
				upAngle -= rotationSpeed * dT;
				rotationOccur = true;
			}

			if(currentSpeed > maxSpeed)
			    currentSpeed = maxSpeed;
			else if(currentSpeed < -maxSpeed)
			    currentSpeed = -maxSpeed;

			if(rotationOccur)
				rotation = Matrix.CreateFromAxisAngle(up, upAngle);

			Vector3 direction = new Vector3(0.0f, 0.0f, -1.0f);
			direction = Vector3.Transform(direction, rotation);

//			currentSpeed -= drag * currentSpeed * dT;
			position += direction * currentSpeed * dT;

			translation = Matrix.CreateTranslation(position);
		}
	}
}